/**
 * 
 */
package com.ao.andou.common;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import android.text.TextUtils;
import android.util.Log;

/**
 * Provide some connection utility method
 * @author pentiumao
 *
 */
public class ConnectionUtils {

    private final static String TAG = "ConnectionUtils";
    
    public final static String doGet(String url) {

        Log.d("doGet", url);
        if (TextUtils.isEmpty(url)) {
            throw new IllegalArgumentException("url is empty");
        }

        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(url);
        // TODO: timeout process
        
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = null;
        try {
            responseBody = httpclient.execute(httpget, responseHandler);
        } catch (ClientProtocolException e) {
            Log.e(TAG, "ClientProtocolException", e);
        } catch (IOException e) {
            Log.e(TAG, "IOException", e);
        } finally {
            httpclient.getConnectionManager().shutdown();
        }
        return responseBody;
    }
}
